import numpy
from obstacle import *
from math import sqrt

class Sphere(Obstacle):
    """The class representing a sphere."""
    
    state           = 0;
    speed           = 0;
    resizeSpeed     = 0;
    radius          = 0;
    sqrRadius       = 0;
    futureRadius    = 0;
    slices          = 0;
    stacks          = 0; 
    
    middle          = numpy.zeros((3),   dtype=float);
    dir             = numpy.zeros((3),   dtype=float); 
    
    def __init__(self, mX, mY, mZ, radius):
        self.resizeSpeed  = 1;
        self.slices       = 25;
        self.stacks       = 25;
        self.radius       = radius;
        self.sqrRadius    = radius * radius;
        self.futureRadius = radius;
        self.middle       = numpy.array([mX, mY, mZ]); 
    def StaticMove(self, dx, dy, dz):
        tempArray = numpy.array([dx, dy, dz]);
        self.middle += tempArray;    
    def CollisionHandle(self, point):
        tempArray  = point - self.middle;
#        dotProduct = tempArray[0] * tempArray[0] + tempArray[1] * tempArray[1] + tempArray[2] * tempArray[2];

        dotProduct = numpy.dot(tempArray, tempArray);
        
        if dotProduct <= self.sqrRadius:
            fac = 1;
            len = sqrt(dotProduct);
            if len != 1:
                u   = numpy.array(
                    [fac * tempArray[0] * (self.radius/len-1), 
                     fac * tempArray[1] * (self.radius/len-1), 
                     fac * tempArray[2] * (self.radius/len-1)]);
                
                point = point + u;
        return point;
    
    def StaticResize(self, resizeCoeff):
        self.radius      *= resizeCoeff;
        self.futureRadius = self.radius;
        self.sqrRadius    = self.radius * self.radius; 
    def Resize(self, speed, factor):
        if speed != 0:
            self.resizeSpeed  = speed;
            self.futureRadius = factor * self.radius;
        else:
            self.resizeSpeed  = 1;
            self.futureRadius = self.radius;
    def Move(self, x, y, z, speed):
        len = sqrt(x*x + y*y + z*z);
        
        if len != 0:
            self.dir = numpy.array([x / len, y / len, z / len]);
        else:
            self.dir = numpy.zeros(3);
        
        self.speed = speed;
    def SetState(self, newState):
        self.state = newState;